<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>下降路径最小和</title>
</head>
<body>
  <script>
    function fn(A) {
      let row = A.length
      let cols = A[0].length
      //1. 层循环同于行的迭代
      //2. 层循环同行列的迭代
      for (let i = 1; i < row; i++) {
        for (j = 0; j < cols; j++) {
          if (j == 0) {
            A[i][j] = Math.min(A[i - 1][j], A[i - 1][j + 1]) + A[i][j]
          }
          else if (j == cols - 1) {
            A[i][j] = Math.min(A[i - 1][j - 1], A[i - 1][j]) + A[i][j]
          } else {
            A[i][j] = Math.min(A[i - 1][j - 1], A[i - 1][j], A[i - 1][j + 1]) + A[i][j]
          }
        }
      }
      return Math.min(...A[row - 1])
    }
    console.log(fn([[-19,57], [-40,-5]]))
  </script>
</body>
</html>